﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CN.DBML;
using CN.Commons;

namespace CityNavigation
{
    public partial class BusLineNav : System.Web.UI.Page
    {
       private CityNavigationDataContext dc = new CityNavigationDataContext(System.Configuration.ConfigurationManager.ConnectionStrings["CityNavigationConnectionString"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            this.createLines();
        }

        /// <summary>
        /// 生成线路集合的Html,替换Literal1.Text
        /// </summary>
        private void createLines()
        {
            StringBuilder sb = new StringBuilder();
            
            Dictionary<string, List<BusLine>> dicLines = new Dictionary<string, List<BusLine>>();
            dicLines = this.getLines();
            foreach (string key in dicLines.Keys)
            {
                sb.Append("<table>");
                sb.Append("<tr>");
                sb.Append("<td colspan='6'>");

                sb.Append(string.Format("<input value='{0}' id='{0}' style='cursor: pointer;color:#3366CC;background-color:White;border-style:Groove;font-size:14pt;font-weight:bold;height:30px;width:30px;' />", key));
                sb.Append("</td>");
                sb.Append("</tr>");
                
                for (int i = 0; i < dicLines[key].Count/6; i++)
                {
                    sb.Append("<tr>");
                    for (int j = 0; j < 6; j++)
                    {
                        if (i * 6 + j > dicLines[key].Count) break;
                        sb.Append("<td>");
                        sb.Append(string.Format("<input value='{0}' id='{0}' style='cursor: pointer;color:#B0ADB8;background-color:#FFFFFF;border-style:Groove;font-size:8pt;font-weight:bold;height:20px;width:70px;' onclick=\"showLine('{0}')\"/>&nbsp;&nbsp;&nbsp;", dicLines[key][i * 6 + j].LineName));
                        //sb.Append();
                        sb.Append("</td>");
                         
                    }
                    sb.Append("</tr>");
                }

                sb.Append("</table>");

                sb.Append("<br/>");
                sb.Append("<br/>");
            }
            this.Literal1.Text = sb.ToString();
        }
        /// <summary>
        /// 取出公交路线集合
        /// </summary>
        /// <returns></returns>
        private Dictionary<string, List<BusLine>> getLines()
        {
            //公交路线字典
            Dictionary<string, List<BusLine>> dicLines = new Dictionary<string, List<BusLine>>();
            //创建一个数组，来存储所有的线路索引
            string[] headArr = new string[14] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "B", "K", "Q", "T", "Y" };
            //根据线路索引循环取出线路索引所对应的线路集合
            foreach (string headStr in headArr)
            {
                string tempHeadStr = headStr;
                if (headStr == "Q")
                {
                    tempHeadStr = "区域";
                }
                
                var lines = from item in dc.BusLines
                            where item.LineName.StartsWith(tempHeadStr.ToString())
                            select item;
                List<BusLine> listLines = new List<BusLine>();
                foreach (var item in lines)
                {
                    listLines.Add(item);
                }
                dicLines.Add(headStr.ToString(), listLines);
            }

            return dicLines;
        }
    }
}